//
//  AlbumViewController.m
//  SocialEngine
//
//  Created by Nguyen Ba Tan on 5/25/13.
//  Copyright (c) 2013 Home. All rights reserved.
//

#import "AlbumViewController.h"
#import "BrowserAlbumCell.h"
#import "PageSetting.h"
#import "WebServices.h"
#import "XMLParser.h"
#import "BrowserAlbum.h"
#import "XMLDataCommon.h"
#import "defines.h"

@interface AlbumViewController ()

@end

@implementation AlbumViewController {
    PageSetting *pageSetting;
    XMLDataCommon *xmlData;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

- (void) updateTableView:(XMLDataCommon *) _xmlData{
    xmlData = _xmlData;
    [self.tableAlbum reloadData];
    
}

- (void) getAlbumsData {
    NSString *strUrlHttp = [NSString stringWithFormat:@"%@/album/getalbums?ItemPerPage=%d&PageId=%d&type=all",
                            URL_MAIN, pageSetting.iItemPerPage, pageSetting.iPage];
    NSLog(@"%@", strUrlHttp);
    NSData *responseData = [WebServices getXmlDataHttp:strUrlHttp];
    
    if (responseData) {
        XMLParser *parse = [[XMLParser alloc] initXMLParser:PARSE_BROWSE_ALBUM];
		[parse doParse:responseData];
        
        [self performSelectorOnMainThread:@selector(updateTableView:) withObject:parse.xmlData waitUntilDone:NO];
    }
}

- (void) viewWillAppear:(BOOL)animated {
    // Init value for page
    
    if (!xmlData) {
        xmlData = [[XMLDataCommon alloc] init];
    }
    
    if (!pageSetting) {
        pageSetting = [[PageSetting alloc] init];
    }
    [pageSetting initpageValue];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self performSelectorInBackground:@selector(getAlbumsData) withObject:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setTableAlbum:nil];
    [super viewDidUnload];
}

#pragma mark -
#pragma mark Tableview delegate, datasource method
 
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return [xmlData.itemList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BrowserAlbumCell *cell = (BrowserAlbumCell *)[tableView
                                      dequeueReusableCellWithIdentifier:@"AlbumCell"];
	
    BrowserAlbum *albumData = [xmlData.itemList objectAtIndex:indexPath.row];
	cell.lbAlbumName.text = albumData.AlbumTitle;
	cell.lbAlbumDesc.text = albumData.AlbumDescription;
    
    if (cell.imgAlbumThmb.image == nil) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:albumData.AlbumAvatarURL]];
        cell.imgAlbumThmb.image = [UIImage imageWithData:data];
    }

    return cell;
}

@end
